const app = getApp()
Page({
  data: {

  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1,
    })
  },
  async getUserInfo() {
    let res = await wx.getUserProfile({
      desc: '用于分专业方向',
    })
    this.setData({
      nickName: res.userInfo.nickName,
      avatarUrl: res.userInfo.avatarUrl
    })
  },
  async formsubmit({ detail }) {
    if (!this.data.nickName) {
      wx.showToast({
        icon: 'error',
        title: '请获取微信账号',
      })
      return
    }
    if (!detail.value.studentName || !detail.value.studentNum) {
      wx.showToast({
        icon: 'error',
        title: '请输入学号和姓名',
      })
      return
    }
    if (!/^\d{8}$/.test(detail.value.studentNum)) {
      wx.showToast({
        icon: 'error',
        title: '请输入8位数字学号',
      })
      return
    }

    let nickName = this.data.nickName;
    let avatarUrl = this.data.avatarUrl;

    let res = await wx.cloud.callFunction({
      name: "register",
      data: {
        sn: detail.value.studentNum, name: detail.value.studentName, nickName, avatarUrl
      }
    })
    if (res.result.reg == 'ok') {
      wx.showToast({
        icon: 'success',
        title: '绑定成功',
      })
      app.globalData.user = res.result.user
      wx.navigateTo({
        url: './rank',
      })
    } else {
      wx.showModal({
        cancelColor: 'cancelColor',
        title: res.result.reg,
        content: res.result.errMsg
      })
    }
  }
})